package cn.etynit.pet.controller;

import cn.etynit.basic.util.LoginContext;
import cn.etynit.org.domain.Employee;
import cn.etynit.org.service.EmployeeService;
import cn.etynit.pet.dto.AcceptDto;
import cn.etynit.pet.dto.AllocationDto;
import cn.etynit.pet.dto.ReturnDto;
import cn.etynit.pet.service.ISearchMasterMsgService;
import cn.etynit.pet.domain.SearchMasterMsg;
import cn.etynit.pet.query.SearchMasterMsgQuery;
import cn.etynit.basic.util.AjaxResult;
import cn.etynit.basic.util.PageList;
import cn.etynit.user.domain.Logininfo;
import cn.etynit.user.domain.User;
import cn.etynit.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                // 已完成订单页面添加默认状态为3
                searchMasterMsg.setState(3);
                // 默认shopId为超级管理员所在店铺id
                searchMasterMsg.setShopId(39L);
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 宠物寻主
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request)
    {
        // 获取到logininfo
        Logininfo logininfo = LoginContext.getLogininfo(request);
        // 通过logininfo的id找到user对象
        User user = userService.findByLoginId(logininfo.getId());
        // 将添加SearchMasterMsg表里面的userId
        searchMasterMsg.setUserId(user.getId());
        return searchMasterMsgService.publish(searchMasterMsg);
    }


    /**
     * 查询待审核数据
     * state = 0，没有shopId
     * 提交了寻主信息，待审核的接口
     * @param SearchMasterMsg
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery SearchMasterMsg)
    {
        // 状态state设置为0，待处理
        SearchMasterMsg.setState(0);
        // 返回待处理数据
        return searchMasterMsgService.selectSearchMasterMessageByParam(SearchMasterMsg);
    }


    /**
     * 查询店铺待处理数据
     * state = 1
     * shopId = 当前登录人的id
     * 审核通过，已分配到店铺，接单拒单的接口
     * @param SearchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery SearchMasterMsg,HttpServletRequest request)
    {
        // 获取当前登录人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 寻主信息添加shopId
        SearchMasterMsg.setShopId(employee.getShopId());
        // 状态state设置为1，待处理
        SearchMasterMsg.setState(1);
        // 返回待处理数据
        return searchMasterMsgService.selectSearchMasterMessageByParam(SearchMasterMsg);
    }


    /**
     * 查询寻主池的接口
     * state = 1
     * shopId = null
     * 平台已经审核完了，但是没有匹配到商家，进入寻主池
     * @param SearchMasterMsg
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery SearchMasterMsg)
    {
        // 状态state设置为1，待处理
        SearchMasterMsg.setState(1);
        // 返回数据
        return searchMasterMsgService.selectSearchMasterMessageByParam(SearchMasterMsg);
    }

    /**
     * 已完成表的接口
     * state = 3
     * shopId = null是平台管理员，我们不需要管，shopId = 店铺id，则设置在订单上
     * @param SearchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery SearchMasterMsg,HttpServletRequest request)
    {
        // 获取到当前的员工对象
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 等于null，那么就是平台管理员，那么shopId我们就是要null，那么就不需要管


        // 如果当前的员工的shopId存在，则需要设置订单的shopId
        if (null != employee.getShopId()){
            SearchMasterMsg.setShopId(employee.getShopId());
        }
        // 订单状态设置问3
        SearchMasterMsg.setState(3);
        // 返回数据
        return searchMasterMsgService.selectSearchMasterMessageByParam(SearchMasterMsg);
    }


    /**
     * 拒单
     * @param msgId
     * @param request
     * @return
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request)
    {
        // 查到当前登录员工
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 拒单方法
        searchMasterMsgService.reject(msgId,employee);
        return AjaxResult.me().success();
    }


    /**
     * 接单
     * @param acceptDto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto)
    {
        searchMasterMsgService.accept(acceptDto);
        return AjaxResult.me().success();
    }


    /**
     * 审核
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/risePetMsg/{id}")
    public AjaxResult risePetMsg(@PathVariable("id")Long id, HttpServletRequest request)
    {
        // 查到当前登录员工
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 审核方法
        searchMasterMsgService.risePetMsg(id,employee);
        return AjaxResult.me().success();
    }

    /**
     * 审核不通过
     * @param returnDto
     * @param request
     * @return
     */
    @PostMapping("/returnMsg")
    public AjaxResult returnMsg(@RequestBody ReturnDto returnDto, HttpServletRequest request)
    {
        // 查到当前登录员工
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 审核不通过方法
        searchMasterMsgService.returnMsg(returnDto,employee);
        return AjaxResult.me().success();
    }


    /**
     * 分配店铺
     * @param allocationDto
     * @param request
     * @return
     */
    @PutMapping("/allocation")
    public AjaxResult allocation(@RequestBody AllocationDto allocationDto, HttpServletRequest request)
    {
        // 查到当前登录员工
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 审核不通过方法
        searchMasterMsgService.allocation(allocationDto,employee);
        return AjaxResult.me().success();
    }
}
